var xmlHttpRequest;
function createXMLHttpRequest()
{
    if (window.XMLHttpRequest) //非IE浏览器
    {
        xmlHttpRequest = new XMLHttpRequest();
    }
    else if (window.ActiveObject)//IE6以上版本的IE浏览器
    {
        xmlHttpRequest = new ActiveObject("Msxml2.XMLHTTP");
    }
    else //IE6及以下版本IE浏览器
    {
        xmlHttpRequest = new ActiveObject("Microsoft.XMLHTTP");
    }
}

/*function updateCart() {
    var count = document.getElementById("tab").rows.length-3; //总行数-不相关的行
    var tableId = document.getElementById("tab");
    var quantity = new Array(count);
    var listprice = document.getElementById("listprice").children[0].value;
    var i=1;
    while (i<=count){
        quantity[i-1]=tableId.rows[i].cells[3].children[0].value;
        i++;
    }
    var array = quantity.join(',');
    var subTotal = 0;
    for (var j=0;j<array.length;j++){
        newTotalCost = array[j].value * listprice;
        console.log(newTotalCost);
        subTotal += newTotalCost;
        document.getElementById("total").children[0].value(newTotalCost);
    }
    console.log(subTotal);
    document.getElementById("subtotal").children[0].value(subTotal);
}*/

function updateCart() {
    var count = document.getElementById("tab").rows.length-3; //总行数-不相关的行
    var tableId = document.getElementById("tab");
    var quantity = new Array(count);
    var listprice = [[${cart.listprice}]];
    var i=1;
    while (i<=count){
        quantity[i-1] = [[${cart.quantity}]];
        i++;
    }
    var array = quantity.join(',');
    var subTotal = 0;
    for (var j=0;j<array.length;j++){
        newTotalCost = array[j].value * listprice;
        console.log(newTotalCost);
        subTotal += newTotalCost;
        document.getElementById("total").children[0].value(newTotalCost);
    }
    console.log(subTotal);
    document.getElementById("subtotal").children[0].value(subTotal);
}

function sendRequest(url) {
    createXMLHttpRequest();
    xmlHttpRequest.open("GET", url, true);
    xmlHttpRequest.onreadystatechange = processResponse;
    xmlHttpRequest.send(null);
}

function processResponse() {
    if (xmlHttpRequest.readyState == 4) {
        if (xmlHttpRequest.status == 200) {
            var resp = xmlHttpRequest.responseText;
            var array = resp.split(",");

            var quantity = document.getElementById("quantity");
            var total = document.getElementById("total");
            var subtotal = document.getElementById("subtotal");

            var count = document.getElementById("tab").rows.length-3;
            var tableId = document.getElementById("tab");
            var i=1;
            var k=0;
            while (i<=count){
                tableId.rows[i].cells[3].getElementsByClassName("im").innerText = array[k];
                tableId.rows[i].cells[5].innerText = array[k+1];
                //alert(quantity[i]);
                i++;
                k=k+2;
            }
            subtotal.innerText = array[array.length-1];
        }
    }
}
